import { Message } from 'element-ui';
const HUBNAME = 'MyuSignalr';


const updateOrderPrint = {
    name:'updateOrderPrint',
    method:function(data){
        console.log(data)
    }
}


/*服务器调用客户端方法*/
// 打印新订单
const sendMessage = {
    name:'sendMessage',
    method:function(data,message){
       console.log(`在${data}收到消息${message}`)
    }
}

const get = {
    name:'Get',
    method:function(data){
        console.log(data)
    }
}


//服务器端的方法
const serverMethodSets = [updateOrderPrint];
//客户端的方法
const clientMethodSets = [sendMessage,get]; //将需要注册的方法放进集合


// 建立连接
export function startConnection() {
    let hub = $.hubConnection("http://localhost:6676")
    let proxy = createHubProxy(hub) //需要先注册方法再连接
    hub.start().done((connection) =>{
        console.log('Now connected, connection ID=' + connection.id)
    }).fail(()=>{
        Message('连接失败');
        console.log('Could not connect');
    })
    hub.error(function (error) {
        Message('SignalR error: ' + error);
        console.log('SignalR error: ' + error)
    })
    hub.connectionSlow(function () {
        console.log('We are currently experiencing difficulties with the connection.')
    });
    hub.disconnected(function () {
        console.log('disconnected')
    });
    return proxy
}
// 手动创建proxy
export function createHubProxy(hub){
    let proxy = hub.createHubProxy(HUBNAME)
    return proxy
}